package com.example.forum.utils;

import com.example.forum.model.User;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

@Component
//md5加密工具类
public class MD5Utils {

    //md5加密
    public static String md5(String str) {
        return DigestUtils.md5DigestAsHex(str.getBytes(StandardCharsets.UTF_8));
    }

    //
    public static String md5Salt(String password, String salt) {
        //1.对原密码md5加密
        String password1 = md5(password);
        //2.返回 password1 + salt 组合而成的字符串进行md5加密
        return md5(password1 + salt);
    }

    public static Boolean verify(String password, User user) {
        String inputPassword = MD5Utils.md5Salt(password, user.getSalt());
        return inputPassword.equals(user.getPassword());
    }

}
